﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    public class AdminUserInfoController : AdminBaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.User _userService = new RDBMS.User();
        #endregion

        // GET: Admin/AdminUserInfo
        public ActionResult Index()
        {
            return View();
        }
        //密码修改页面
        public ActionResult UpdataDw()
        {
            return View();
        }
        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdatePW(string NewPW, string OldPW, string SecPW)
        {
            if (NewPW != SecPW)
            {
                return Json(new { code = 1, msg = "修改失败，两次密码输入不一致！" });
            }
            bool result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryUpdateUserPassWd(out result, CurrentUser.UserID, NewPW, OldPW))
                {
                    return Json(new { code = 0, msg = "用户密码修改成功" });
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }

        /// <summary>
        /// 后台用户添加页面
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public ActionResult InsterPage(int UserId)
        {
            User[] UserInfos;
            using (var conn = new RDBMS.User())
            {
                conn.TrySelectUser(out UserInfos, false, UserId);
            }
            return View(UserInfos[0]);
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminUpdate(User model)
        {
            var models = new User[] { model };
            bool result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryUpdateUser(out result, models))
                {
                    return Json(new { code = 0, msg = "用户修改成功" });
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
        /// <summary>
        /// 分页获取文章数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserByPage(int page, int limit)
        {
            User[] UserInfos;
            int TotalCount;
            using (var conn = new RDBMS.User())
            {
                if (conn.TrySelectUserByPage(out UserInfos, out TotalCount, page, limit))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = UserInfos });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<User>() });
        }
        /// <summary>
        /// 用户添加操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AdminInsertPage()
        {
            return View();
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminInsert(User model)
        {
            //先判断用户账号是否存在
            var accounts = new string[] { model.UserAccount };
            bool accresult;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryGetIsAccountExist(out accresult, accounts))
                {
                    if (accresult)
                    {
                        return Json(new { code = 1, msg = "添加失败，用户已存在！" });
                    }
                }
                else
                {
                    return Json(new { code = 1, msg = "添加失败，请稍后重试！" });

                }

            }
            model.IsAdmin = 0;
            model.IsDelete = false;
            model.PhoneState = 0;
            model.EmailState = 0;
            model.HeadPortrait = "/Content/layui/images/8.jpg";
            var models = new User[] { model };
            bool result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryInsertUser(out result, models))
                {
                    return Json(new { code = 0, msg = "用户添加成功" });
                }
            }
            return Json(new { code = 1, msg = "添加失败，请稍后重试！" });
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminDelete(int[] ids)
        {
            if (ids != null)
            {
                bool userResult;
                using (var conn = new RDBMS.User())
                {
                    if (conn.TryDeleteUser(out userResult, ids))
                    {
                        return Json(new { code = 0, msg = "删除成功！" });
                    }
                }
            }
            return Json(new { code = 1, msg = "删除失败，请稍后重试！" });
        }

        /// <summary>
        /// 设置管理员
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetAdmin(int userID, int Isadmin)
        {
            bool userResult;
            bool c = Isadmin==0 ? true : false;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryUpdateUserIsAdmin(out userResult, userID, IsAdmin: c))
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
    }
}